package cn.edu.scau.cmi.zhangxiyang.client;

import cn.edu.scau.cmi.zhangxiyang.Singleton.chairman;
import cn.edu.scau.cmi.zhangxiyang.Singleton.marshal;
import java.util.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;  
import javafx.stage.Stage;  
import javafx.scene.layout.*;
import javafx.scene.control.Button;  
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;  
import javafx.stage.Stage;  
import javafx.scene.Scene;  

public class SingletonClient extends Application{
	String name = "";
	public void start(Stage primaryStage) {
		
		chairman ch = null;
		chairman ch1 = chairman.getMarry("毛泽东");
		TextField text3 = new TextField();
		Set<marshal> marshalset = new HashSet<marshal>();
		Stage stage = new Stage();
		GridPane gp = new GridPane();
		Button btn3 = new Button("主席上任");
		Button btn4 = new Button("元帅上任");
		TextField text1 = new TextField();
	    TextField text2 = new TextField();
		Label label1 = new Label("现任主席：毛泽东");
		String name1 = text1.getText(),name2 = text2.getText();
		gp.setVgap(20);
		gp.setHgap(15);
		gp.setPadding(new Insets(25,25,25,25));
		
		gp.addRow(0, label1);
		gp.addRow(1, text1,btn3);
		gp.addRow(2, text2,btn4);
		gp.addRow(3, text3);
		
		btn3.setOnAction(e -> {  
			if(name1 != null) show(ch1,ch,name1);
			if(show(ch1,ch,name1)) {
				text3.setText("现任主席为："+ch1.getName());
			}
			else text3.setText("不一样");
        });
		
		btn4.setOnAction(e -> {  
			if(text2.getText() != null) {
				text3.setText("现在有  " + show1(marshalset,name2) + " 个元帅");
			}
        });
		
		stage.setScene(new Scene(gp,300, 200));
        stage.setTitle("单例多例模式");
        stage.show();
	}
	
	public static void main(String args[]) {
		launch(args);
	}
	
	public boolean show(chairman ch1,chairman ch,String name)
	{
	    ch = chairman.getMarry(name);
		
		if(ch1 == ch) {
			System.out.println("一样");
			System.out.println("主席："+ch1.getName());
			return true;
		}
		else {
			System.out.println("不一样");
			return false;
		}
	}
	
	public int show1(Set<marshal> marshalset,String name) {
		marshalset = marshal.givebirth(name);
		System.out.println("现在有  " + marshalset.size() + " 个元帅");
		return marshalset.size();
	}

}
